using UnityEngine;
using System.Collections;
using OMGThieves;

public class Player : MonoBehaviour 
{	
	public CharacterStats cStats;
	
	private float attackTimer;
	public float movSpeed;
	public int damage;
    public float attackSpeed;
	public int mana;
	private bool acting;

	public Material[] materials;
	
	public Enemy target;
	
	void Start()
	{
		rigidbody.velocity = Vector3.zero;

		this.cStats.strength = PlayerPrefs.GetInt("playerStrength");
		this.cStats.agility = PlayerPrefs.GetInt("playerAgility");
		this.cStats.vitality = PlayerPrefs.GetInt("playerVitality");
		this.cStats.energy = PlayerPrefs.GetInt("playerEnergy");
	}
	
	public void whenChangeWave ()	// Chame isso quando mudar de wave
	{
        this.cStats.strength = PlayerPrefs.GetInt("playerStrength");
        this.cStats.agility = PlayerPrefs.GetInt("playerAgility");
        this.cStats.vitality = PlayerPrefs.GetInt("playerVitality");
        this.cStats.energy = PlayerPrefs.GetInt("playerEnergy");

		this.damage = 50 + (this.cStats.strength * (20 / 99));
		this.attackSpeed = 3 - (this.cStats.agility * (2.85f / 99));
		this.movSpeed = 4 + (this.cStats.agility * (9 / 99));
		this.mana = 50 + (this.cStats.energy * (100 / 99));
	}

	void Update () 
	{
        if (Input.GetKeyDown(KeyCode.Insert))
        {
            PlayerPrefs.DeleteAll();
            whenChangeWave();
        }

        //Debug.Log("P: " + movSpeed + "/" + damage + "/" + attackSpeed);
		Vector3 dir = Vector3.zero;
		
    	dir.x = Input.acceleration.x * movSpeed;
    	dir.z = Input.acceleration.y * movSpeed;
		
		dir *= Time.deltaTime;
		
		transform.Translate(dir);
		
		transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * movSpeed * Time.deltaTime);
		transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * movSpeed * Time.deltaTime);
		
		if(target == null)
		{
			renderer.material = materials[0];
		}
		
		if(acting)
		{
			Attack();
		}
		
		if(Input.GetKeyDown(KeyCode.Escape))
		{
			Application.Quit();
		}
	}
	
	void OnCollisionEnter (Collision other)
	{
		if (other.gameObject.tag == "Enemy")
		{
			target = other.gameObject.GetComponent<Enemy>();
            gameObject.layer = 11;
            other.gameObject.layer = 11;
			Fight(target);
		}
	}
	
	void OnCollisionExit (Collision other)
	{
		if (other.gameObject.tag == "Enemy")
		{
			unFight(target);
			target = null;
            gameObject.layer = 10;
            other.gameObject.layer = 9;
		}
	}
	
	void Fight (Enemy target)
	{
        materials[1] = target.gameObject.renderer.material;
		renderer.material = materials[2];
		target.gameObject.renderer.material = materials[2];
		
		acting = true;
		target.setActing(true);
	}
	
	void unFight(Enemy target)
	{
		renderer.material = materials[0];
		acting = false;

        if (target != null)
        {
            target.gameObject.renderer.material = materials[1];
            target.setActing(false);
        }
	}
	
	void Attack()
	{
		attackTimer += Time.deltaTime;
		
		if(attackTimer > attackSpeed)
		{
			target.loseHp(damage);
			attackTimer = 0;
		}
	}
}